<template>
  <div class="fullpage">
    <div v-if="!showForm">
      <h2>用户管理</h2>
      <div class="toolbar">
        <el-input v-model="keyword" placeholder="搜索用户名" @keyup.enter="load" clearable />
        <el-button type="primary" @click="load">搜索</el-button>
        <el-button type="success" @click="showAddForm">新增用户</el-button>
      </div>
      <el-table :data="users" style="width: 100%;">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="role" label="角色" width="100" />
        <el-table-column prop="balance" label="余额" width="100" />
        <el-table-column label="操作" width="160">
          <template #default="scope">
            <el-button size="small" @click="editUser(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="delUser(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="page"
        :page-size="size"
        :page-sizes="[5, 10, 20]"
        :total="total"
        layout="prev, pager, next, jumper, sizes"
        @current-change="load"
        @size-change="handleSizeChange"
        style="margin-top: 16px; text-align: right;"
      />
    </div>
    <div v-if="showForm">
      <div class="form-header">
        <h2>{{ editForm.id ? '编辑用户' : '新增用户' }}</h2>
        <el-button @click="backToList">返回列表</el-button>
      </div>
      <el-form :model="editForm" :rules="rules" ref="formRef" label-width="80px" style="width: 100%;">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editForm.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="!editForm.id">
          <el-input v-model="editForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="editForm.role" style="width: 100%;">
            <el-option label="管理员" value="ADMIN" />
            <el-option label="普通用户" value="USER" />
          </el-select>
        </el-form-item>
        <el-form-item label="余额" prop="balance">
          <el-input-number v-model="editForm.balance" :min="0" style="width: 100%;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveUser">保存</el-button>
          <el-button @click="backToList">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
const users = ref([])
const page = ref(1)
const size = ref(10)
const total = ref(0)
const keyword = ref('')
const showForm = ref(false)
const editForm = ref({ id: null, username: '', password: '', role: 'USER', balance: 0 })
const formRef = ref()
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'change' }],
  balance: [{ required: true, message: '请输入余额', trigger: 'blur' }]
}
const load = async () => {
  const res = await axios.get('/api/users/page', { params: { page: page.value, size: size.value, keyword: keyword.value } })
  users.value = res.data.data.records
  total.value = res.data.data.total
}
onMounted(load)
function showAddForm() {
  showForm.value = true
  editForm.value = { id: null, username: '', password: '', role: 'USER', balance: 0 }
}
function editUser(u) {
  showForm.value = true
  editForm.value = { ...u, password: '' }
}
function delUser(id) {
  axios.delete('/api/users/' + id).then(() => { ElMessage.success('删除成功'); load() })
}
function saveUser() {
  formRef.value.validate(async valid => {
    if (!valid) return
    if (editForm.value.id) {
      await axios.put('/api/users', editForm.value)
      ElMessage.success('修改成功')
    } else {
      const { id, ...data } = editForm.value
      await axios.post('/api/users', data)
      ElMessage.success('新增成功')
    }
    backToList(); load()
  })
}
function backToList() {
  showForm.value = false
  editForm.value = { id: null, username: '', password: '', role: 'USER', balance: 0 }
  if (formRef.value) formRef.value.resetFields()
}
function handleSizeChange(val) {
  size.value = val
  page.value = 1
  load()
}
</script>
<style>
.fullpage {
  width: 100%;
  height: 100%;
  background: #f5f6fa;
  padding: 0;
  box-sizing: border-box;
}
.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
</style> 